package com.yic.module.system.controller.admin.logger;

import com.yic.framework.common.pojo.CommonResult;
import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.excel.core.util.ExcelUtils;
import com.yic.framework.operatelog.core.annotations.OperateLog;
import com.yic.module.system.controller.admin.logger.vo.externallog.*;
import com.yic.module.system.convert.logger.ExternalLogConvert;
import com.yic.module.system.dal.dataobject.logger.ExternalLogDO;
import com.yic.module.system.service.logger.ExternalLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.yic.framework.common.pojo.CommonResult.success;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 第三方API调用日志")
@RestController
@RequestMapping("/system/external-log")
@Validated
public class ExternalLogController {

    @Resource
    private ExternalLogService externalLogService;

    @PostMapping("/create")
    @Operation(summary = "创建第三方API调用日志")
    @PreAuthorize("@ss.hasPermission('system:external-log:create')")
    public CommonResult<Long> createExternalLog(@Valid @RequestBody ExternalLogCreateReqVO createReqVO) {
        return success(externalLogService.createExternalLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新第三方API调用日志")
    @PreAuthorize("@ss.hasPermission('system:external-log:update')")
    public CommonResult<Boolean> updateExternalLog(@Valid @RequestBody ExternalLogUpdateReqVO updateReqVO) {
        externalLogService.updateExternalLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除第三方API调用日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:external-log:delete')")
    public CommonResult<Boolean> deleteExternalLog(@RequestParam("id") Long id) {
        externalLogService.deleteExternalLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得第三方API调用日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:external-log:query')")
    public CommonResult<ExternalLogRespVO> getExternalLog(@RequestParam("id") Long id) {
        ExternalLogDO externalLog = externalLogService.getExternalLog(id);
        return success(ExternalLogConvert.INSTANCE.convert(externalLog));
    }

    @GetMapping("/list")
    @Operation(summary = "获得第三方API调用日志列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('system:external-log:query')")
    public CommonResult<List<ExternalLogRespVO>> getExternalLogList(@RequestParam("ids") Collection<Long> ids) {
        List<ExternalLogDO> list = externalLogService.getExternalLogList(ids);
        return success(ExternalLogConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得第三方API调用日志分页")
    @PreAuthorize("@ss.hasPermission('system:external-log:query')")
    public CommonResult<PageResult<ExternalLogRespVO>> getExternalLogPage(@Valid ExternalLogPageReqVO pageVO) {
        PageResult<ExternalLogDO> pageResult = externalLogService.getExternalLogPage(pageVO);
        return success(ExternalLogConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出第三方API调用日志 Excel")
    @PreAuthorize("@ss.hasPermission('system:external-log:export')")
    @OperateLog(type = EXPORT)
    public void exportExternalLogExcel(@Valid ExternalLogExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ExternalLogDO> list = externalLogService.getExternalLogList(exportReqVO);
        // 导出 Excel
        List<ExternalLogExcelVO> datas = ExternalLogConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "第三方API调用日志.xls", "数据", ExternalLogExcelVO.class, datas);
    }

}
